package cn.lanqiao.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.entity.Score;
import java.awt.Font;

public class TeacherSource extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private JButton btnUpdate;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TeacherSource frame = new TeacherSource();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	
	/**
	 * Create the frame.
	 */
	public TeacherSource() {
		setTitle("学生成绩");
		setClosable(true);
		setBounds(100, 100, 689, 384);
		this.setLocation(150, 50);
		getContentPane().setLayout(null);
		JScrollPane scrollPane=new JScrollPane();
		scrollPane.setBounds(0, 0, 689, 190);
		getContentPane().add(scrollPane);
		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		//设置表格数据居中
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
		cr.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, cr);
		table.setRowHeight(30);
		cn.lanqiao.service.ScoreService service =new cn.lanqiao.service.impl.ScoreServiceImpl();
		Object[][] datas = service.getAllScore();
		String[] strs = new String[] {"学号", "课程编号", "\u6210\u7EE9"};
		table.setModel(new DefaultTableModel(datas,strs));
		
		
		scrollPane.setViewportView(table);
		
		JButton btnNewButton = new JButton("添加学生成绩");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//弹出添加对话框
				TeacherAddSourceFrm frm=new TeacherAddSourceFrm(table);
				frm.setVisible(true);
				
//				table.setModel(new DefaultTableModel(
//						service.lists(),
//						new String[] {
//							"\u7F16\u53F7", "\u5B66\u53F7", "\u6210\u7EE9"
//						}
//					));
//				
			}
		});
		btnNewButton.setBounds(43, 272, 125, 49);
		getContentPane().add(btnNewButton);
		
		btnUpdate = new JButton("修改学生成绩");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//判断用户是否选择要修改的联系人
				int count=table.getSelectedRowCount();
				if(count==0) {
					JOptionPane.showMessageDialog(null, "请选择要修改的联系人");
					return;
				}
				
				//拿要修改的联系人的信息
				String sid,scourse,sgrade;
				int rowIndex=table.getSelectedRow();
				
				sid=table.getValueAt(rowIndex,0).toString();
				scourse=table.getValueAt(rowIndex,1).toString();
				sgrade=table.getValueAt(rowIndex,2).toString();
				
				//窗体传递数据
				Score score=new Score(sid,scourse,sgrade);
				
               //弹出对话框
				TeacherUpdateSourceFrm frm=new TeacherUpdateSourceFrm(score,table);
				frm.setVisible(true);
				table.setModel(new DefaultTableModel(
						service.getAllScore(),
						new String[] {
							"学号", "课程编号", "\u6210\u7EE9"
						}
					));

				
			}
		});
		btnUpdate.setBounds(268, 272, 125, 49);
		getContentPane().add(btnUpdate);
		
		JButton btnNewButton_2 = new JButton("删除学生成绩");
		btnNewButton_2.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				//删除确认
				int val = JOptionPane.showConfirmDialog(null, "确认删除吗？", "删除确认", JOptionPane.YES_OPTION);
				if(val==1) {
					return ;
				}
				//实现删除
				int rowIndex=table.getSelectedRow();
				//获取单元格数据
				String id=table.getValueAt(rowIndex, 0).toString();
				//实现删除
				cn.lanqiao.service.ScoreService service=new cn.lanqiao.service.impl.ScoreServiceImpl();
				service.deleteUserByid(id);
				//刷新窗体中的table数据
				
				table.setModel(new DefaultTableModel(
						service.getAllScore(),
						new String[] {
							"学号", "课程编号", "\u6210\u7EE9"
						}
					));
			}
		});
		btnNewButton_2.setBounds(496, 272, 118, 49);
		getContentPane().add(btnNewButton_2);
	}

}
